//
//  HomeViewController.swift
//  SinaWeiBo
//
//  Created by Leon 笑 竹 on 2017/7/31.
//  Copyright © 2017年 Leon 笑 竹. All rights reserved.
//

import UIKit
import AFNetworking

fileprivate let cellID = "cellID"

class HomeViewController: BaseViewController {
    
    //加载状态列表数据
    fileprivate lazy var statusListViewModel = StatusListViewModel()
    
    //懒加载数组
    fileprivate lazy var statusList  = [String]()
    
    //重写数据源方法
    override func loadData() {
        
        print("最后一条数据 \(self.statusListViewModel.statusList.last?.text ?? " ")")
        statusListViewModel.loadStatus(isPullup: self.isPullUp) { (isSuccess, hasMorePullup) in
            
            print("数据加载结束")
            
            self.refreshControl?.endRefreshing()
            self.isPullUp = false
            
            //标识为真再刷新表格
            if hasMorePullup {
                self.tableView?.reloadData()
            }
        }
        
//        NetworkManager.shared.statusList { (list, success) in
//            print(list ?? "")
//        }
        
        //网络状态请求
        let networkStatus = NetWorkStatus.shared.reachAbilityStatus()
        print(networkStatus)
        
        //加载数据
//        print("开始加载数据 (\(NetworkManager.shared))")
        
        //模拟延迟加载数据
//        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
//
//            for i in 0..<20 {
//                
//                if self.isPullUp {
//                    //上拉
//                    self.statusList.append("上拉 \(i)")
//                    
//                }else{
//                    //下拉
//                    self.statusList.insert(i.description, at: 0)
//                }
//            }
//            
//            self.isPullUp = false
//            self.tableView?.reloadData()
//            self.refreshControl?.endRefreshing()
//        }
        
    }
    
    func showFriend() {
        print(#function)
        
        let demoVC = DemoViewController()
        demoVC.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(demoVC, animated: true)
        
    }    
}

extension HomeViewController{
    
    override func setupTableView() {
        
        super.setupTableView()
    
        naviItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, target: self, action: #selector(showFriend),isBack : false)
        
        //注册原型cell
//        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        tableView?.register(UINib.init(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: cellID)
        
    }
}

//重写tableView方法
extension HomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return statusList.count
        return statusListViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
//        cell.textLabel?.text = statusList[indexPath.row]
        cell.textLabel?.text = statusListViewModel.statusList[indexPath.row].text
        return cell
    }
    

}


